/**
 * Created by Administrator on 2016/9/8 0008.
 */
var request = require("request");
var cherrio = require("cheerio");
var iconv = require("iconv-lite");
var fs = require("fs");
var url = require("url");
//var logger = require("../utils/myLog4.js");

function getValue(ele,value){
    var retobj = {};
    for(var i=0;i<value.length;i++){
        var type = value[i].type;
        var attribute = value[i].attribute;
        var name = value[i].name?value[i].name:i;
        switch(type){
            case "text":retobj[name] = ele.text();
                break;
            case "html":retobj[name] = ele.html();
                break;
            case "attr":retobj[name] = ele.attr(attribute);
                break;
        }
    }

    var attrs = getObjectAttrs(retobj);
    if(attrs.length === 0){
        return "";
    }else if(attrs.length === 1){
        return retobj[attrs[0]];
    }else{
        return retobj;
    }
}
//获取object的属性列表
function getObjectAttrs(obj){
    var ret = [];
    for(var item in obj){
        ret.push(item);
    }
    return ret;
}

function sGet(settings,cb){
    var num = 0;
    function _sGet(settings,callback){
        var webUrl = settings.url;
        var encode = settings.encode?settings.encode:"utf8";
        var content = settings.content?settings.content:[];
        var retryNum = settings.retryNum?settings.retryNum:5;
        var ret = {};
        request({
            "method":"get",
            "url":webUrl,
            "encoding": null,  // 关键代码
            "timeout":5000
        },function(err,res){
            if(!err&&res.statusCode === 200){
                //http正常返回
                ret["webIndex"] = res.request.uri.href;
                var body = iconv.decode(res.body, encode);
                var $ = cherrio.load(body,{decodeEntities: false});
                for(var i=0;i<content.length;i++){
                    var item = content[i];
                    var name = item.name;
                    var selector = item.selector;
                    var eq = item.eq;
                    var value = item.value;
                    var eles ;
                    if(typeof eq  === "number"){
                        eles = $(selector).eq(eq);
                    }else{
                        eles = $(selector);
                    }
                    if(eles.length === 1){
                        ret[name] = getValue(eles,value);
                    }else{
                        ret[name] =[];
                        for(var j=0;j<eles.length;j++){
                            var myVal = getValue(eles.eq(j),value)
                            myVal.index = j+1;
                            ret[name].push(myVal);
                        }
                    }
                }
                callback(err,ret);
            }else{
                if(num<retryNum){
                    num++;
                    _sGet(settings,callback);
                 }else{
                    err?(err[err] = "访问出现问题。"):(err="访问出现问题。");
                    callback(err,ret);
                 }
            }

        });
    }
    _sGet(settings,cb);
}
exports.sGet = sGet;
